<?php 
defined ('_JEXEC') or die ('Restricted access');
jimport('joomla.application.component.model');
jimport('joomla.filesystem.file');
class HotrosModelHotro extends JModel
{
	var $_id = null;
	var $_data = null;
	function __construct(){
		parent::__construct();
		$array = JRequest::getVar('cid',  0, '', 'array');		
		$edit	= JRequest::getVar('edit',true);
		if($edit)
		$this->setId((int)$array[0]);
	}
	function setId($id){
		$this->_id		= $id;
		$this->_data	= null;
	}
	function store()	{	
		$row =& $this->getTable('hotro'); 
		$data = JRequest::get( 'post' );
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		if($row->id ==0){
			$db = &JFactory::getDBO();
			$query = "SELECT max(ordering) FROM #__hotro";
			$db->setQuery($query);
			$row->ordering = $db->loadResult() +1;
		}
		if (!$row->store()) {
			$this->setError( $row->getErrorMsg() );
			return false;
		}
		if (!$row->hotro_id) {
			$where = 'parent_id = ' . (int) $row->parent_id ;
			$row->ordering = $row->getNextOrder( $where );
		}	
		return true;
	}
	function delete(){
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$row =& $this->getTable();
		if (count( $cids )) {
			foreach($cids as $cid) {
				if (!$row->delete( $cid )) {
					$this->setError( $row->getErrorMsg() );
					return false;
				}
			}
		}
		return true;
	}
	/*function support($cid = array()){		
		$db =& JFactory::getDBO();
		$result = false;
		if(count($cid)){			
			foreach($cid as $key=>$value){				
				$query = "UPDATE #__hotro set support='1' WHERE hotro_id =".$value;					
				$db->setQuery( $query );
				if (!$db->query())
				{
					$this->setError($this->_db->getErrorMsg());
					return false;
				}				
			}
		}		
		return true;		
	}*/
	function publish($cid = array(), $publish = 1){
		if (count( $cid )){
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );
			$query = 'UPDATE #__hotro'
				. ' SET published = '.(int) $publish
				. ' WHERE hotro_id IN ( '.$cids.' )';
			$this->_db->setQuery( $query );
			if (!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}
		return true;
	}
	function move($direction){   	
		$row =& $this->getTable('hotro');
		if (!$row->load($this->_id)) {
			$this->setError($this->_db->getErrorMsg());
			return false;			
		}
		if (!$row->move( $direction  )) {
			$this->setError($this->_db->getErrorMsg());
			return false;		
		}
		return true;
	}
	function saveorder($cid = array(), $order)	{
		$row =& $this->getTable('hotro');
		$groupings = array();
		for( $i=0; $i < count($cid); $i++ )		{
			$row->load( (int) $cid[$i] );
			$groupings[] = $row->parent_id;
			if ($row->ordering != $order[$i])			{
				$row->ordering = $order[$i];
				if (!$row->store()) {
					$this->setError($this->_db->getErrorMsg());
					return false;
				}
			}
		}
		$groupings = array_unique( $groupings );
		foreach ($groupings as $group){
			$row->reorder('parent_id = '.(int) $group);
		}
		return true;
	}
	function &getData(){
		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__hotro '.
					'  WHERE hotro_id = '.$this->_id;
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->hotro_id = 0;
			$this->_data->nickname1 = null;
			$this->_data->nickname3 = null;
			$this->_data->published = 1;
			$this->_data->ordering = null;
			$this->_data->parent_id  = null;
			$this->_data->nameyh  = null;
			$this->_data->namesky  = null;
			$this->_data->support  = null;
			$this->_data->hotline = null;
			$this->_data->hotline1 = null;
		}
		return $this->_data;
	}
}
?>
